package com.jqbd.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jqbd.pojo.Company;
import com.jqbd.service.CompanyService;
import com.jqbd.util.JsonResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;



@RestController
@RequestMapping("company")
public class CompanyController {
    @Resource
    private CompanyService companyService;

    @RequestMapping("findAll")
    public JsonResult<Company> findAll() {
        List<Company> companyList = companyService.findAll();

        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setData(companyList);
        return jsonResult;
    }

    @RequestMapping("findByPage")
    public JsonResult<Company> findByPage(
            @RequestParam(value = "page",required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "limit",required = false,defaultValue = "3") Integer pageSize

    ){
        PageHelper.startPage(pageNum,pageSize);

        List<Company> companyList = companyService.findAll();

        PageInfo<Company> patientPageInfo = new PageInfo<>(companyList);

        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setCount(patientPageInfo.getTotal());
        jsonResult.setData(patientPageInfo.getList());
        jsonResult.setCode(0);
        return jsonResult;

    }

    @RequestMapping("add")
    public void add(Company company, HttpServletResponse response, HttpServletRequest request) throws IOException {

        System.out.println(company);

        companyService.add(company);

        // 重定向到后端
        response.sendRedirect(request.getContextPath() + "/home.html");
    }


    /**+
     * 修改企业信息
     * @param company 企业对象
     * @return
     */
    @RequestMapping("update")
    public String update(Company company) {

        System.out.println(company);

        companyService.update(company);

        return "success";
    }

    /**
     * 删除单个
     * @param companyId 企业编号
     * @return
     */
    @RequestMapping("deleteById")
    public String deleteById(Integer companyId) {

        companyService.deleteById(companyId);

        return "success";
    }


    /**
     * 批量删除企业信息
     * @param companyIds 学历编号数组对象
     * @return
     */
    @RequestMapping("deleteByIds")
    public String deleteByIds(Integer[] companyIds) {


        if (companyIds.length > 0) {
            companyService.deleteByIds(companyIds);
        }
        return "success";
    }

}
